package com.friend.street.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.friend.street.dto.JsonResult;
import com.friend.street.dto.JsonResultType;
import com.friend.street.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 736440967696110274L;
	

	protected ServletContext getContext() {
		return ServletActionContext.getServletContext();
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 根据参数获取值
	 * 
	 * @param name
	 * @return
	 */
	protected String getParameter(String name) {
		String value = getRequest().getParameter(name);
		return StringUtil.dealNullOrEmptyString(value);
	}
	/**
	 * 设置参数的值
	 * @param name
	 * @param value
	 */
	protected void setAttribute(String name,Object value){
		getRequest().setAttribute(name, value);
	}
	/**
	 * 输出文本
	 * @param text
	 */
	protected void printText(String text) {
		try {
			getResponse().setContentType("text/plain;charset=utf-8");
			PrintWriter out = getResponse().getWriter();
			out.print(text);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 输出含有业务逻辑的json
	 * @param result
	 */
	protected void printJsonText(JsonResult result){
		try {
			getResponse().setContentType("text/plain;charset=utf-8");
			PrintWriter out = getResponse().getWriter();
			out.print(result.toJson());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 检查验证码是否正确
	 * @param validateNumber
	 * @return true:验证码正确;false:验证码不正确
	 */
	protected boolean checkValidateNumber(String validateNumber) {
		return validateNumber.equals(getSession().getAttribute("validateNumber"));
	}
}
